package com.bonc.utilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.io.FileUtils;

import com.bonc.vectorspace.model.EventDocument;

/**
 * @author donggui@bonc.com.cn
 * @version 2016 2016年6月27日 下午3:35:14
 */
public class EventUtility {
	
	private static String delimeter = "\n|。|？|！";
	
	private static Map<String, String> stopwords = loadStopwords();
	
	private static String[] sentenceSegment(String content){
	if(content!=null){
		return content.split(delimeter);
	}
	return null;
}
	
	private static List<EventDocument> convertFile2Document(String fname, String fpath, String tag){
		File file = new File(fpath+fname);
		List<EventDocument> documents = new ArrayList<EventDocument>();
		String content;
		try {
			content = FileUtils.readFileToString(file);
			String[] events = sentenceSegment(content);
			for(int index = 0; index < events.length; index++){
				String docId = fname + index;
				String candidate = events[index];
				System.out.println(docId+"=="+candidate);
				if(candidate!=null && !"".equals(candidate.trim())){
					EventDocument document = new EventDocument(docId,candidate,tag,stopwords);
					if(document!=null){
						documents.add(document);						
					}
				}				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return documents;		
	}
	
	/**
	 * @author donggui@bonc.com.cn
	 * @param topPath, use filename(including many events) as the tag
	 */
	public static List<EventDocument> convertFiles2Document(String topPath){
		
		List<String> fileNames = FileUtility.getFileList(topPath);
		
		List<EventDocument> documents = new ArrayList<EventDocument>();
        
		for(String fname:fileNames){
			//use filename as the tag of instance contained
			String tag = fname.replaceAll(".txt", "");
			documents.addAll(convertFile2Document(fname, topPath, tag));
		}
		return documents;		
	}
	
	
	public static List<EventDocument> convertText2Document( String content,String tag){
		List<EventDocument> documents = new ArrayList<EventDocument>();
		String[] sentences = sentenceSegment(content);
		int validCount = 0;
		for(int i=0; i< sentences.length; i++){
			String sent = sentences[i];
			if(sent!=null && !"".equals(sent.trim())){ 
				EventDocument document = new EventDocument(String.valueOf(validCount), sentences[i], tag, stopwords);
				documents.add(document);
				validCount++;
			}
		}
		return documents;
	}

	public static HashMap<String, String> convertFiles2Text(String topPath){
		
		List<String> fileNames = FileUtility.getFileList(topPath);
		
//		List<EventDocument> documents = new ArrayList<EventDocument>();
		HashMap<String,String> texts = new HashMap<String,String>();
        
		for(String fname:fileNames){
			//use filename as the tag of instance contained
			String tag = fname.replaceAll(".txt", "");
			
			File file = new File(topPath+fname);
			String content;
			try {
				content = FileUtils.readFileToString(file);
				String[] sentences = EventUtility.sentenceSegment(content);
				for(String sent:sentences){
					texts.put(sent, tag);
//					System.out.println("test2.3=="+sent);
				}

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		System.out.println("test2.2=="+texts.size());
		return texts;		
	}
	
	public static Map<String, String> loadStopwords(){
		
		Map<String, String> stopwords = new ConcurrentHashMap<String, String>();
		try {
			System.out.println("[LOAD] Load stopwords.");
			//		String filePath = getDictsDir() + "stopwords.txt";
			String filePath = "C:\\Users\\Administrator\\git\\hierarchical-clustering-java\\src\\main\\resources\\"+ "stopwords.txt";
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(filePath));
			BufferedReader br = new BufferedReader(reader);

			String line = null;
			while ((line = br.readLine()) != null) {
				stopwords.put(line, "");
			}

			br.close();
			reader.close();
			System.out.println("[LOAD] Load stoprwords finished.");
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return stopwords;
	}
}
